package cn.cqvie.controller;

import cn.cqvie.common.R;
import cn.cqvie.pojo.Employee;
import cn.cqvie.pojo.EmployeeRole;
import cn.cqvie.service.EmployeeService;
import cn.cqvie.service.EmployeeRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@CrossOrigin
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private EmployeeRoleService roleService;
    @GetMapping("/getMSG")
    public R<Page> getPage(int page,int pageSize,String name){
        Page<Employee> myPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getCreateTime);
        employeeService.page(myPage,queryWrapper);
        return R.success(myPage);
    }
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        Employee e =(Employee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        employee.setUpdateUser(e.getId());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @DeleteMapping
    public R<String> deleteEmployee(Long id){
        LambdaQueryWrapper<EmployeeRole> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(EmployeeRole::getEmployeeId,id);
        roleService.remove(Wrapper);
        employeeService.removeById(id);
        return R.success("删除成功");
    }
    @PostMapping("/addEmployee")
    public R<String> add(HttpSession session,@RequestBody Employee employee){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password = encoder.encode("123456");
        Employee e =(Employee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        employee.setCreateUser(e.getId());
        employee.setUpdateUser(e.getId());
        employee.setEnabled(true);
        employee.setAccountNonLocked(true);
        employee.setAccountNonExpired(true);
        employee.setCredentialsNonExpired(true);
        employee.setPassword(password);
        employeeService.save(employee);

        EmployeeRole employeeRole = new EmployeeRole();
        employeeRole.setRoleId(3L);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee e2 = employeeService.getOne(queryWrapper);
        employeeRole.setEmployeeId(e2.getId());
        roleService.save(employeeRole);
        return R.success("添加成功");
    }
    @GetMapping("/getEmployee")
    public R<Employee> editEmployee(Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}
